package client.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import client.PhotoClient;
import client.PhotoLiveEvent;
import client.PhotoSelectResultBean;
import client.R;
import client.util.BGAPhotoHelper;
import client.util.BGAPhotoPickerUtil;

/**
 * 项目: client.activity
 * 创建人: LuoFG
 * 创建时间: 2021/7/12  16:41
 * 描述:
 */
public class BGATakePhotoActivity extends AppCompatActivity {
    private static final String EXTRA_CAMERA_FILE_DIR = "EXTRA_CAMERA_FILE_DIR";
    private static final String EXTRA_CAMERA_CROP = "EXTRA_CAMERA_CROP";

    /**
     * 拍照的请求码
     */
    private static final int REQUEST_CODE_TAKE_PHOTO = 1;
    private static final int REQUEST_CODE_CROP = 2;//裁剪


    private BGAPhotoHelper mPhotoHelper;
    public static PhotoLiveEvent<PhotoSelectResultBean> photoResultData = new PhotoLiveEvent<>();//获取返回结果
    public static PhotoLiveEvent<Boolean> photoResultCancel = new PhotoLiveEvent<>();//取消拍照
    private boolean isCrop = false;//默认不支持裁剪

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取拍照图片保存目录
        try {
            File cameraFileDir = (File) getIntent().getExtras().getSerializable(EXTRA_CAMERA_FILE_DIR);
            if (cameraFileDir != null) {
                mPhotoHelper = new BGAPhotoHelper(cameraFileDir);
            } else {
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }

        if (getIntent().getExtras().containsKey(EXTRA_CAMERA_CROP)) {
            isCrop = getIntent().getExtras().getBoolean(EXTRA_CAMERA_CROP);
        }

        takePhoto();
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        BGAPhotoHelper.onSaveInstanceState(mPhotoHelper, outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        BGAPhotoHelper.onRestoreInstanceState(mPhotoHelper, savedInstanceState);
    }


    /**
     * 拍照
     */
    private void takePhoto() {
        try {
            startActivityForResult(mPhotoHelper.getTakePhotoIntent(), REQUEST_CODE_TAKE_PHOTO);
        } catch (Exception e) {
            BGAPhotoPickerUtil.show(R.string.bga_pp_not_support_take_photo);
        }
    }

    /**
     * 返回已选中的图片集合
     *
     * @param selectedPhotos
     */
    private void returnSelectedPhotos(ArrayList<String> selectedPhotos) {
        //采用观察者模式 返回数据
        photoResultData.postValue(new PhotoSelectResultBean(PhotoClient.getClient().getPhotoResultTag(), selectedPhotos));
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //拍照返回
            if (requestCode == REQUEST_CODE_TAKE_PHOTO) {
                //需要裁剪
                if (isCrop) {
                    try {
                        startActivityForResult(mPhotoHelper.getCropIntent(mPhotoHelper.getCameraFilePath(), 800, 800), REQUEST_CODE_CROP);
                    } catch (Exception e) {
                        mPhotoHelper.deleteCameraFile();
                        mPhotoHelper.deleteCropFile();
                        BGAPhotoPickerUtil.show(R.string.bga_pp_not_support_crop);
                        e.printStackTrace();
                    }
                }
                //不裁剪则直接返回
                else {
                    ArrayList<String> photos = new ArrayList<>(Arrays.asList(mPhotoHelper.getCameraFilePath()));
                    returnSelectedPhotos(photos);
                }
            }
            //裁剪返回
            else if (requestCode == REQUEST_CODE_CROP) {
                ArrayList<String> photos = new ArrayList<>(Arrays.asList(mPhotoHelper.getCropFilePath()));
                returnSelectedPhotos(photos);
            }
        } else {
            if (requestCode == REQUEST_CODE_CROP) {
                mPhotoHelper.deleteCameraFile();
                mPhotoHelper.deleteCropFile();
            }
            if (resultCode == RESULT_CANCELED) {
                //采用观察者模式 返回数据
                photoResultCancel.postValue(true);
                //取消则关闭
                finish();
            }
        }
    }


    public static class IntentBuilder {
        private Intent mIntent;

        public IntentBuilder(Context context) {
            mIntent = new Intent(context, BGATakePhotoActivity.class);
        }

        /**
         * 拍照后图片保存的目录。如果传 null 表示没有拍照功能，如果不为 null 则具有拍照功能，
         */
        public BGATakePhotoActivity.IntentBuilder cameraFileDir(@Nullable File cameraFileDir) {
            mIntent.putExtra(EXTRA_CAMERA_FILE_DIR, cameraFileDir);
            return this;
        }

        /**
         * 保存图片的目录，如果传 null，则没有保存图片功能
         */
        public BGATakePhotoActivity.IntentBuilder crop(boolean isCrop) {
            mIntent.putExtra(EXTRA_CAMERA_CROP, isCrop);
            return this;
        }


        public Intent build() {
            return mIntent;
        }
    }
}
